package com.jh.user.business.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jh.user.business.UserCoinBusiness;
import com.jh.user.pojo.RankingList;
import com.jh.user.pojo.UserAccount;
import com.jh.user.pojo.UserCoinHistory;
import com.jh.user.repository.RankingListRepository;
import com.jh.user.repository.UserAccountRepository;
import com.jh.user.repository.UserCoinRepository;

@Service
public class UserCoinBusinessImpl implements UserCoinBusiness{

	@Autowired
	private UserCoinRepository userCoinRepository;
	
	@Autowired
	private UserAccountRepository accountRepository;
	
	@Autowired
	private RankingListRepository rankingListRepository;
	
	@Autowired
	private EntityManager em;
	
	@Override
	public Page<UserCoinHistory> queryUserCoinHistoryByUserid(long userid,
			Pageable pageAble) {
		return userCoinRepository.findCoinHistoryByUserid(userid, pageAble);
	}

	@Override
	public UserCoinHistory saveUserCoinHistory(UserCoinHistory userCoinHistory) {
		return userCoinRepository.save(userCoinHistory);
	}

	@Transactional
	@Override
	public UserAccount updateUserCoin(UserAccount userAccount, int coin,  String  addorsub, String ordercode) {
		
		UserAccount olduserAccount = accountRepository.findUserAccountByUseridLock(userAccount.getUserId());
		

		int curcoin;
		if(addorsub.equalsIgnoreCase("0")){
			
			curcoin = userAccount.getCoin()+coin;
			
		}else{
			
			curcoin = userAccount.getCoin()-coin;
			
		}
		
		if(curcoin < 0 ){
			curcoin = 0;
		}
		
		olduserAccount.setCoin(curcoin);
		UserAccount newAcct = accountRepository.save(olduserAccount);	
			
		UserCoinHistory  userCoinHistory  = new UserCoinHistory();			
		userCoinHistory.setAddOrSub(addorsub);
		userCoinHistory.setOrdercode(ordercode);
		userCoinHistory.setCoin(coin);
		userCoinHistory.setCreateTime(new Date());
		userCoinHistory.setCurCoin(curcoin);
		userCoinHistory.setUserId(userAccount.getUserId());
		userCoinRepository.save(userCoinHistory);
		em.flush();
		return newAcct;
	}

	@Override
	public List<RankingList> queryRankingListCoin(long brandid) {
		
		
		return rankingListRepository.queryRankingListCoin(brandid);
	}

	@Override
	public List<RankingList> queryRankingListEarnings(long brandid) {
		
		return rankingListRepository.queryRankingListEarnings(brandid);
	}
	@Transactional
	@Override
	public RankingList addRankingList(RankingList rankingList) {
		
		return rankingListRepository.save(rankingList);
	}
	
	@Override
	public RankingList queryRankingListByid(long id) {
		
		return rankingListRepository.queryRankingListByid(id);
	}

	//根据brandid和status获取一定时间段内的信息
	@Override
	public int findFullNameByStatus(long brandId, ArrayList<String> status, Date startTimeDate, Date endTimeDate) {
		return userCoinRepository.findFullNameByStatus(brandId, status, startTimeDate, endTimeDate);
	}
	//根据brandid和status获取一定时间段内的信息
	@Override
	public int findFullNameByStatus(ArrayList<String> status, Date startTimeDate, Date endTimeDate) {
		return userCoinRepository.findFullNameByStatus(status, startTimeDate, endTimeDate);
	}
	
}
